package com.yiguo.logistics.app;

import org.apache.http.client.HttpClient;

import com.yiguo.common.CellInfoManager;
import com.yiguo.common.CellLocationManager;
import com.yiguo.common.Dialogs;
import com.yiguo.common.LoaderGPRSLocation;
import com.yiguo.common.PubConst;
import com.yiguo.common.WifiInfoManager;
import com.yiguo.logistics.app.R;
import com.yiguo.logistics.entity.model.ModOrder;
import com.yiguo.logistics.web.LoaderOrderDetails;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ActBarcode extends Activity
{

   private Button btnScanCode;
   private Button btnInputCode;
   private TextView txtAddress;
   private Button btnGPRS;
   private Button btnGPRD_WIFI;
  // private ListView listView;

   // LoaderOrderDetails loader;
   ModOrder modOrder;
   Dialogs dialogs;

   // private String searchResult;
   private String barCode;

   // private String address;

   void InitControls()
   {
	  btnScanCode = (Button) findViewById(R.id.btnScanCode);
	  btnInputCode = (Button) findViewById(R.id.btnInputCode);
	  txtAddress = (TextView) findViewById(R.id.txtAddress_Act_barcode);
	  btnGPRS = (Button) findViewById(R.id.btnGPRS);
	  btnGPRD_WIFI=(Button)findViewById(R.id.button1);
	//  listView=(ListView)findViewById(R.id.listView1);
   }

   void InitControlListener()
   {
	  btnScanCode.setOnClickListener(new OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			// TODO Auto-generated method stub
			Intent intent = new Intent("com.google.zxing.client.android.SCAN");
			startActivityForResult(intent, PubConst.ActivityCode.SCAN_CODE);
		 }
	  });
	  btnInputCode.setOnClickListener(new OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.setClass(ActBarcode.this, ActInputcode.class);
			startActivityForResult(intent, PubConst.ActivityCode.INPUT_CODE);
		 }
	  });
	  btnGPRS.setOnClickListener(new OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			// TODO Auto-generated method stub
			txtAddress.setVisibility(View.VISIBLE);
			LoaderGPRSLocation loader = new LoaderGPRSLocation(ActBarcode.this, dialogs, txtAddress);
			loader.execute((Void) null);
		 }
	  });
	  btnGPRD_WIFI.setOnClickListener(new OnClickListener()
	  {
	     @Override
	     public void onClick(View v)
	     {
	  	  // TODO Auto-generated method stub
			txtAddress.setVisibility(View.VISIBLE);
//	    	CellInfoManager cellManager = new CellInfoManager(ActBarcode.this);
//            WifiInfoManager wifiManager = new WifiInfoManager(ActBarcode.this);
//            CellLocationManager locationManager = new CellLocationManager(ActBarcode.this, cellManager, wifiManager) {
//                   @Override
//                   public void onLocationChanged() {
//                	  txtAddress.setText(this.latitude() + "-" + this.longitude());
//                          this.stop();
//                   }
//            };
//            locationManager.start();
			
//			HttpClient httpClient = new HttpClient();
//
//			httpClient.setsetConnectionTimeout(5000);
//			getOffset(httpClient,25620917,55392414);
	     }
	  });
   }

//   public static boolean getOffset(HttpClient httpClient, int lat, int lng) {
//
//		StringBuilder url = new StringBuilder();
//
//		url.append("http://ditu.google.com/maps/vp?");
//
//		url.append("spn=0.0,0.0&z=18&vp=");
//
//		url.append(lat / 100D);
//
//		url.append(",");
//
//		url.append(lng / 100D);
//
//		String urlStr = url.toString();
//		
//		GetMethod method = new GetMethod(urlStr);
//
//		try {
//
//		int status = httpClient.executeMethod(method);
//
//		if (status == 200) {
//
//		byte[] bs = method.getResponseBody();
//
//		String js = new String(bs, "utf-8");
//
//		int x = js.lastIndexOf("[");
//
//		int y = js.lastIndexOf("]");
//
//		if (x > 0 && y > 0) {
//
//		String text = js.substring(x + 1, y);
//
//		int b = text.lastIndexOf(",");
//
//		int a = text.lastIndexOf(",", b - 1);
//
//		if (a > 0 && b > 0) {
//
//		String offsetPixX = text.substring(a + 2, b);
//
//		String offsetPixY = text.substring(b + 2);
//
////		OffsetBean offset = new OffsetBean();
////
////		offset.setLng(lng);
////
////		offset.setLat(lat);
////
////		offset.setOffset_x(Integer.parseInt(offsetPixX));
////
////		offset.setOffset_y(Integer.parseInt(offsetPixY));
////
////		OffsetBDB offsetBDB = OffsetBDB.getInstance();
////
////		offsetBDB.putOffset(offset);
//
//		return true;
//
//		}
//
//		} else {
//		
//		System.out.println("error 1锛�  lat:" + lat + "\tlng:" + lng);
//
//		}
//
//		} else {
//
//		
//		System.out.println("error 2锛�  lat:" + lat + "\tlng:" + lng);
//
//		}
//
//		} catch (Exception e) {
//
//		e.printStackTrace();
//
//		} finally {
//
//		method.releaseConnection();
//
//		}
//
//		return false;
//
//		}

   
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.act_barcode);
	  dialogs = new Dialogs(ActBarcode.this);
	  InitControls();
	  InitControlListener();
   }

   @Override
   protected void onResume()
   {
	  super.onResume();
   }

   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent returnIntent)
   {
	  ModOrder mOrder;
	  switch (requestCode)
	  {
		 case PubConst.ActivityCode.SCAN_CODE:
			if (resultCode == RESULT_OK) // 找到产品
			{
			   barCode = returnIntent.getStringExtra("SCAN_RESULT");
			   String url = PubConst.Url.GetOrderBySerialNumber;
			   url = String.format(url, barCode);

			   Intent intent = new Intent();
			   intent.setClass(ActBarcode.this, ActOrderDetails.class);

			   LoaderOrderDetails loader = new LoaderOrderDetails(url, dialogs, barCode, intent, ActBarcode.this, PubConst.ActivityCode.BARCODE);
			   loader.execute((Void) null);
			}
			else
			{
			   dialogs.ShowOKAlertDialog("", "没有获取到条码号");
			}
			break;
			
		 case PubConst.ActivityCode.INPUT_CODE:
			if (resultCode == RESULT_OK)
			{
			   mOrder = (ModOrder) returnIntent.getSerializableExtra("order");
//			   dialogs.ShowOKAlertDialog("从Input返回", "流水号为" + mOrder.getSerialNumber());
			}
			else 
			{
			     
			}
			break;
		 case PubConst.ActivityCode.ORDER_DETAILS:
			if (resultCode == RESULT_OK)
			{
			   mOrder = (ModOrder) returnIntent.getSerializableExtra("order");
//			   dialogs.ShowOKAlertDialog("从OrderDetails返回", "流水号为" + mOrder.getSerialNumber());
			}
			else 
			{
			   
			}
			break;
	  }
   }

   // 退出提示框
   void FinishDialog()
   {
	  DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {
			// TODO Auto-generated method stub
			dialog.dismiss();
			android.os.Process.killProcess(android.os.Process.myPid());
		 }
	  };

	  dialogs.ShowOKCancelAlertDialog("提示", "您打算退出程序吗？", listener);
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
	  { // 按下的如果是BACK，同时没有重复
	  // if(loader!=null)
	  // {
	  // loader.cancel(true);
	  // }
		 FinishDialog();
	  }
	  return false;
   }
}